<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends API_Controller {


	public function __construct() {
    	parent::__construct();
	}

	// protected $_rules = array(
	// 	'index' => array(
	// 		array('limit',array('limit','error'),'trim|integer'),
	// 		array('page', array('page','error'),'trim|integer')
	// 	),
	// 	'publish' => array(
	// 		array('title', array('title','error'),'trim|required'),
	// 		array('intro', array('intro','error'),'trim|required')
	// 	)
	// );


	public function index()
	{
		$limit = $this->input->post('limit');
		$page = $this->input->post('page');

		$limit = $limit ? $limit : 10;
		$page = $page ? $page : 1;
		$offset = ($page -1 ) * $limit;

		$this->load->model('lists');
		$list = $this->lists->get_list($limit, $offset);

		$data = array();
		if(!empty($list)){
			$this->load->model('like');
			$this->load->model('laud');
			$this->load->model('comment');
			foreach($list as $l){
				$tmp = array();
				$tmp = array(
					'id' => $l['id'],
					'title' => $l['title'],
					'intro' => $l['intro'],
					'publish_time' => $l['publish_time'],
					'nick' => $l['nick'],
					'user_pic' => $l['user_pic'],
					'like' => 0,
					'comment' => 0,
					'laud' => 0
				);

				$tmp['like'] = $this->like->get_num($l['id']);
				$tmp['laud'] = $this->laud->get_num($l['id']);
				$tmp['comment'] = $this->comment->get_num($l['id']);

				$data[] = $tmp;
			}
		}

		$this->to_api_message(1, '获取列表数据成功', $data, count($data));
	}

	public function publish(){

		$title = $this->input->post('title');
		$intro = $this->input->post('intro');

		$this->load->model('lists');
		$insertData = array();
		$insertData = array(
			'title' => $title,
			'intro' => $intro,
			'user_id' => 1,
			'publish_time' => date('Y-m-d H:i:s')
		);
		$insert_id = $this->lists->publish($insertData);

		if($insert_id){
			$this->to_api_message(1, '发布成功/失败');
		}else{
			$this->to_api_message(0, '发布失败');
		}
	}

	public function my_list(){

		$user_id = 1;
		$this->load->model('lists');
		$list = $this->lists->get_my_list($user_id);

		$data = array();
		if(!empty($list)){
			$this->load->model('like');
			$this->load->model('laud');
			$this->load->model('comment');
			foreach($list as $l){
				$tmp = array();
				$tmp = array(
					'id' => $l['id'],
					'title' => $l['title'],
					'intro' => $l['intro'],
					'publish_time' => $l['publish_time'],
					'like' => 0,
					'comment' => 0,
					'laud' => 0
				);

				$tmp['like'] = $this->like->get_num($l['id']);
				$tmp['laud'] = $this->laud->get_num($l['id']);
				$tmp['comment'] = $this->comment->get_num($l['id']);

				$data[] = $tmp;
			}
		}

		$this->to_api_message(1, '获取我的列表数据成功', $data, count($data));	
	}

	public function my_info(){

		$user_id = 1;
		$this->load->model('user');
		$info = $this->user->get_user_info($user_id);

		$data = array();
		if(!empty($info)){
			$this->load->model('like');
			$this->load->model('laud');
			$this->load->model('comment');
			$data = array(
				'nick' => $info['nick'],
				'user_pic' => $info['user_pic'],
				'like' => 0,
				'comment' => 0,
				'laud' => 0
			);

			$this->load->model('lists');
			$my_list = $this->lists->get_my_list($user_id);
			if(!empty($my_list)){
				foreach($my_list as $list){
					$data['like'] += $this->like->get_num($list['id']);
					$data['laud'] += $this->laud->get_num($list['id']);
					$data['comment'] += $this->comment->get_num($list['id']);
				}
			}
		}

		$this->to_api_message(1, '获取详情信息成功', $data);
	}

	public function statistics(){

		$this->load->model('lists');
		$this->load->model('visitor');
		$data = array();
		$data['list_count'] = $this->lists->get_count();
		$data['visit_count'] = $this->visitor->get_count();

		$this->to_api_message(1, '获取数据成功', $data);
	}

}

/* End of file home.php */
/* Location: ./application/controllers/home.php */